package com.isprint.jslx.lcc.controller;

import com.alibaba.fastjson.JSONObject;
import com.isprint.jslx.jackson.JackSonTool;
import com.isprint.jslx.lcc.global.controller.GlobalController;
import com.isprint.jslx.lcc.model.User;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import redis.clients.jedis.Jedis;

/**
 * Created by ydw on 2017/7/14.
 */
public class IndexController extends GlobalController {

    Cache cache=Redis.use();

    public void index(){
        renderHtml("hello");
    }

    /**
     * 测试mysql和redis
     */
    @Before(Tx.class)
    public void testMysqlAndRedis(){
        Jedis jedis = cache.getJedis();
        User user = new User();
        user.setUsername("user");
        user.setPassword("user");
        user.setLogName("user");
        boolean save = user.save();
        System.out.println(save);
        String jsonString = JSONObject.toJSONString(user);
        String user3 = jedis.set("user", jsonString);
        String user1 = jedis.get("user");
        User parse = JackSonTool.strin2obj(user1, User.class);
        System.out.println(parse.getUsername()+"来自缓存中");
        jedis.close();
        renderHtml("ok");
    }

}
